package com.azh.server.transport;

/**
 * @author zhaozhenhang <zhaozhenhang@kuaishou.com>
 * Created on 2022-05-02
 * Encoder 和 Transporter结合体
 */
public class APackger {


    private Transporter transporter;

    private Encoder encoder;

    public APackger(Transporter transporter, Encoder encoder) {
        this.transporter = transporter;
        this.encoder = encoder;
    }

    public void send(Package pkg) throws Exception {
        byte[] data = encoder.encode(pkg);
        this.transporter.send(data);
    }

    public Package receive() throws Exception {
        byte[] data = transporter.receive();
        return this.encoder.decode(data);
    }

    public void close() throws Exception {
        transporter.close();
    }
}
